package com.example.gty.service.Impl;

import com.example.gty.entity.Grade;
import com.example.gty.entity.Student;
import com.example.gty.entity.StudentVo;
import com.example.gty.entity.UserRole;
import com.example.gty.mapper.GradeMapper;
import com.example.gty.mapper.StudentMapper;
import com.example.gty.mapper.UserRoleMapper;
import com.example.gty.mapper.UsersMapper;
import com.example.gty.service.StudentService;
import com.example.gty.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;


    //按学号查询单个学生成绩信息
    public List<StudentVo> selectStuGradeBySno(String sno) {
        List<StudentVo> list = studentMapper.selectStuGradeBySno(sno);
        return list;
    }

    //查询所有学生 所有成绩
    public ResultVo selectStuAllGrade(String sno) {
        List<StudentVo> studentVos = studentMapper.selectStuAllGrade(sno);
        if (studentVos != null && studentVos.size() > 0) {
            return new ResultVo(1000, "success", studentVos);
        }
        return null;
    }

    //多条件查询学生成绩 性别 班级 专业 学院
    public ResultVo selectStuMulti(String sex, String cclass, String major, String college) {
        List<StudentVo> studentVos = studentMapper.selectStuMulti(sex, cclass, major, college);
        return new ResultVo(1000, "success", studentVos);
    }

    //单科目 修改学生成绩信息
    public ResultVo updateStuGrade(String sno, String cno, Integer score) {
        Example example = new Example(Grade.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sno", sno);
        criteria.andEqualTo("cno", cno);

        Grade grade = new Grade();
        grade.setSno(sno);
        grade.setCno(cno);
        grade.setScore(score);
        grade.setCreateTime(new Date());
        int i = gradeMapper.updateByExample(grade, example);
        if (i > 0) {
            return new ResultVo(1000, "success", null);
        }
        return null;
    }

    //删除学生
    public ResultVo deleteStu(String sno) {
        //删除用户表
        Example example1 = new Example(Grade.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("no", sno);
        int i1 = usersMapper.deleteByExample(example1);
        //删除用户角色表
        int i2 = userRoleMapper.deleteByPrimaryKey(sno);
        //删除学生表
        int i3 = studentMapper.deleteByPrimaryKey(sno);
        //删除成绩表
        Example example = new Example(Grade.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sno", sno);
        int i4 = gradeMapper.deleteByExample(example);
        if (i1 > 0 && i2 > 0 && i3 > 0 && i4 > 0) {
            return new ResultVo(1000, "success", null);
        }
        return null;

    }


    public ResultVo selectCclass() {
        List<String> strings = studentMapper.selectCclasses();
        return new ResultVo(1000,"success",strings);
    }
}
